package com.itheima.controller.report;

import com.itheima.pojo.BusinessReportVO;
import com.itheima.service.ReportService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.MemberReportVO;
import com.itheima.pojo.SetmealReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Encounter
 * @date 2024/10/16 17:18<br/>
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController
    {
        @Autowired
        private ReportService reportService;
        
        /**
         * 获取成员报告
         *
         * @return {@link Result }
         */
        @GetMapping("/getMemberReport")
        public Result getMemberReport()
            {
                try
                    {
                        log.info("开始查询每月会员数量");
                        MemberReportVO memberReportVO = reportService.getMemberReport();
                        if (memberReportVO != null)
                            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, memberReportVO);
                        else
                            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
                    }
            }
        
        /**
         * 获取 SetMeal 报告
         *
         * @return {@link Result }
         */
        @GetMapping("/getSetmealReport")
        public Result getSetmealReport()
            {
                try
                    {
                        log.info("获取套餐占比");
                        SetmealReportVO setmealReportVO = reportService.getSetmealReport();
                        if (setmealReportVO != null)
                            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, setmealReportVO);
                        else
                            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
                    }
            }
        
        /**
         * 获取业务报告数据
         *
         * @return {@link Result }
         */
        @GetMapping("/getBusinessReportData")
        public Result getBusinessReportData()
            {
                try
                    {
                        log.info("开始获取运营数据");
                        BusinessReportVO businessReportVO = reportService.getBusinessReportData();
                        if (businessReportVO != null)
                            {
                                return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, businessReportVO);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
                    }
            }
        
        /**
         * 导出业务报告
         *
         * @param response 响应
         */
        @GetMapping("/exportBusinessReport")
        public void exportBusinessReport(HttpServletResponse response)
            {
                log.info("开始导出数据报表");
                reportService.exportReport(response);
            }
    }
